package com.econ.powercloud.ems.service.biz;

import com.econ.powercloud.common.base.facade.util.JacksonUtil;
import com.econ.powercloud.ems.common.enums.TimeSectionType;
import com.econ.powercloud.ems.dal.entity.EmsArea;
import com.econ.powercloud.ems.dal.entity.EmsAreaTimeSection;
import com.econ.powercloud.ems.dal.entity.EmsEnergyStation;
import com.econ.powercloud.ems.pojo.AreaTimeSectionVO;
import com.econ.powercloud.ems.pojo.TimeSectionVO;
import com.econ.powercloud.ems.service.dao.EmsAreaDAO;
import com.econ.powercloud.ems.service.dao.EmsAreaTimeSectionDAO;
import com.econ.powercloud.ems.service.dao.EmsEnergyStationDAO;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ Description:
 * @ Author：hgp
 * @ Create:2023-02-16-9:06
 */
@Service
@Slf4j
public class AreaTimeSectionBizService {
    @Autowired
    private EmsAreaTimeSectionDAO emsAreaTimeSectionDAO;
    @Autowired
    private EmsEnergyStationDAO emsEnergyStationDAO;
    @Autowired
    private EmsAreaDAO emsAreaDAO;


    public AreaTimeSectionVO get(Long time, String stationId) {
        AreaTimeSectionVO result = new AreaTimeSectionVO();
        EmsEnergyStation emsEnergyStation = emsEnergyStationDAO.selectOneByStationId(stationId);
        Integer city = null;
        Integer province = null;
        List<EmsAreaTimeSection> sections = Lists.newArrayList();
        if (emsEnergyStation!=null){
            city = emsEnergyStation.getCity();
            province = emsEnergyStation.getProvince();
        }
        List<EmsAreaTimeSection> emsAreaTimeSections = emsAreaTimeSectionDAO.selectListByCondition(time, city, province);
        if (CollectionUtils.isEmpty(emsAreaTimeSections)){
            //生效时间内的数据不存在，找本城市其他时间段的数据
            emsAreaTimeSections = emsAreaTimeSectionDAO.selectListByCondition(null,city,province);
            if (CollectionUtils.isEmpty(emsAreaTimeSections)){
                return result;
            }
        }
        EmsAreaTimeSection section = emsAreaTimeSections.get(0);
        Map<Integer, List<EmsAreaTimeSection>> sectionMap = emsAreaTimeSections.stream()
                .collect(Collectors.groupingBy(EmsAreaTimeSection::getCountType));
        sections.addAll(sectionMap.get(section.getCountType()));
        return timeSectionListToVO(sections);
    }

    private AreaTimeSectionVO timeSectionListToVO(List<EmsAreaTimeSection> sections) {
        if (CollectionUtils.isEmpty(sections)){
            return null;
        }
        EmsAreaTimeSection section = sections.get(0);
        AreaTimeSectionVO result = JacksonUtil.convertToObj(section,AreaTimeSectionVO.class);
        List<TimeSectionVO> timeSectionVOList = Lists.newArrayList();
        sections.stream().forEach(d->timeSectionVOList.add(TimeSectionVO.builder()
                .timeType(d.getTimeType()).timeTypeName(TimeSectionType.getDescFromCode(d.getTimeType()))
                .startTime(d.getStartTime()).endTime(d.getEndTime()).build()));
        result.setTimeSectionVOList(timeSectionVOList);
        List<EmsArea> areaList = emsAreaDAO.getListByCityCodeList(Lists.newArrayList(section.getCity(), section.getProvince()));
        Map<Integer, String> cityMap = areaList.stream().collect(Collectors.toMap(EmsArea::getCityCode, EmsArea::getCityName));
        result.setCityName(cityMap.get(result.getCity()));
        result.setProvinceName(cityMap.get(result.getProvince()));
        return result;
    }
}
